#!/usr/bin/env bash

# 生成rsync服务配置文件
cat > /etc/rsyncd.conf <<EOF
# rsync server
# created by lavenliu
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.56.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password

#########################################
[backup]
comment = backup by lavenliu 2016-03-06
path = /backup

[bbs]
comment = bbs by lavenliu 2016-03-06
path = /data0/www/bbs

[blog]
comment = blog by lavenliu 2016-03-06
path = /data0/www/blog
EOF

# 创建共享目录
mkdir /backup
mkdir -p /data0/www/{bbs,blog}

# 创建rsync密码文件并设置权限
useradd rsync -s /sbin/nologin -M
chown -R rsync /backup
echo "rsync_backup:lavenliu" > /etc/rsync.password
chmod 600 /etc/rsync.password

echo "rsync --daemon" >> /etc/rc.local

# 启动rsync服务
rsync --daemon
if [ $? -eq 0 ]; then
    echo -e "\033[34mrsync service is running successfully\033[0m"
else
    echo -e "\033[31mrsync service is not running, please check...\033[0m"
fi
